package com.wgw.first.cart.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wgw.first.R;
import com.wgw.first.app.MainActivity;
import com.wgw.first.base.BaseFragment;
import com.wgw.first.cart.activity.OrderInfoActivity;
import com.wgw.first.cart.adapter.ShopCartAdapter;
import com.wgw.first.cart.utils.CartStorage;
import com.wgw.first.dao.OrderBean;
import com.wgw.first.home.bean.GoodssBean;
import com.wgw.first.utils.Constants;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Admin
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class CartFragment extends BaseFragment {

    private List<GoodssBean.Goods> mGoodsBeanList;
    private TextView tvShopcartEdit;
    private RecyclerView recyclerview;
    private LinearLayout llCheckAll;
    private CheckBox checkboxAll;
    private TextView tvShopcartTotal;
    private Button btnCheckOut;
    private LinearLayout llDelete;
    private CheckBox cbAll;
    private Button btnDelete;
    private Button btnCollection;
    private ImageView ivEmpty;
    private TextView tvEmptyCartTobuy;
    private LinearLayout ll_empty_shopcart;
    private static final int ACTION_EDIT = 0;
    /**
     * 完成状态
     */
    private static final int ACTION_COMPLETE = 1;
    private ShopCartAdapter adapter;

    private void findViews(View view) {
        tvShopcartEdit = (TextView) view.findViewById(R.id.tv_shopcart_edit);
        recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
        llCheckAll = (LinearLayout) view.findViewById(R.id.ll_check_all);
        checkboxAll = (CheckBox) view.findViewById(R.id.checkbox_all);
        tvShopcartTotal = (TextView) view.findViewById(R.id.tv_shopcart_total);
        btnCheckOut = (Button) view.findViewById(R.id.btn_check_out);
        llDelete = (LinearLayout) view.findViewById(R.id.ll_delete);
        cbAll = (CheckBox) view.findViewById(R.id.cb_all);
        btnDelete = (Button) view.findViewById(R.id.btn_delete);
        btnCollection = (Button) view.findViewById(R.id.btn_collection);
        ivEmpty = (ImageView) view.findViewById(R.id.iv_empty);
        tvEmptyCartTobuy = (TextView) view.findViewById(R.id.tv_empty_cart_tobuy);
        ll_empty_shopcart = (LinearLayout) view.findViewById(R.id.ll_empty_shopcart);
        tvEmptyCartTobuy.setClickable(true);
    }

    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.fragment_shoppingcart, null);
        findViews(view);
        return view;
    }

    @Override
    public void initData() {
        //initListener();
        tvShopcartEdit.setTag(ACTION_EDIT);
        tvShopcartEdit.setText("编辑");
        llCheckAll.setVisibility(View.VISIBLE);
        showData();
        initListener();
    }
    private void showData() {
        CartStorage cartStorage = CartStorage.getInstance(mContext);
        List<GoodssBean.Goods> datas = cartStorage.getDataFromLocal();
        System.out.println("cart:");
        System.out.println(datas.toString());
        if (datas != null && datas.size() > 0) {
            tvShopcartEdit.setVisibility(View.VISIBLE);
            adapter = new ShopCartAdapter(mContext, datas, tvShopcartTotal, checkboxAll,cartStorage,cbAll);
            recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerview.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            ll_empty_shopcart.setVisibility(View.GONE);
        } else {
            //显示空的
            tvShopcartEdit.setVisibility(View.GONE);
            ll_empty_shopcart.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置点击监听
     */
    private void initListener() {
        //去结算按钮
        btnCheckOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Constants.ALEADYLOGIN == ""){
                    Toast.makeText(mContext, "请先登录", Toast.LENGTH_SHORT).show();
                }else{
                    //创建一个list<OrderBean>对象，并且进行传输
                    //根据选中了几个checkbox，创建几个OrderBean对象

                    List<OrderBean> list_order = new ArrayList<OrderBean>();
                    //1,先拿到内存中的cartStorge对象
                    List<GoodssBean.Goods> list;
                    list = CartStorage.getInstance(mContext).getDataFromLocal();

                    //2，进行遍历，如果Isselected为true，则创建orderBean对象
                    for (int i = 0; i < list.size(); i++) {
                        GoodssBean.Goods goodsBean = list.get(i);
                        if(goodsBean.isSelected()){
                            //3,将对象添加到list_order中
                            String username = Constants.ALEADYLOGIN;
                            String isbn = goodsBean.getISBN();
                            String address = "";
                            String phone = "";
                            String number = String.valueOf(goodsBean.getNumber());

                            String total_price = String.valueOf((Double.parseDouble(goodsBean.getPrice()))*goodsBean.getNumber());
                           // String total_price = String.valueOf(adapter.getTotalPrice());

                            OrderBean orderBean = new OrderBean(username,isbn,address,phone,number,total_price);
                            list_order.add(orderBean);

                        }
                    }

                    //4，将list添加到intent中
                    Intent intent = new Intent(mContext, OrderInfoActivity.class);
                    intent.putExtra("orderlist",(Serializable) list_order);
                    startActivity(intent);
                }
            }
        });
        tvShopcartEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置编辑的点击事件
                int tag = (int) tvShopcartEdit.getTag();
                if (tag == ACTION_EDIT) {
                    //变成完成状态
                    showDelete();
                } else {
                    //变成编辑状态
                    hideDelete();
                }
            }
        });
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除数据
                adapter.deleteData();
                System.out.println("CartFragment删除后的本地数据");
                System.out.println(CartStorage.getInstance(mContext).getAllData().toString());
                //显示价格
                adapter.showTotalPrice();
                //显示空空如也
                checkData();
                adapter.checkAll();

            }
        });
        tvEmptyCartTobuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MainActivity.class);
                startActivity(intent);
            }
        });
    }




    private void hideDelete() {
        tvShopcartEdit.setText("编辑");
        tvShopcartEdit.setTag(ACTION_EDIT);
        adapter.checkAll_none(true);
        llDelete.setVisibility(View.GONE);
        llCheckAll.setVisibility(View.VISIBLE);
        adapter.showTotalPrice();
    }

    private void showDelete() {
        tvShopcartEdit.setText("完成");
        tvShopcartEdit.setTag(ACTION_COMPLETE);
        adapter.checkAll_none(false);
        cbAll.setChecked(false);
        checkboxAll.setChecked(false);
        llDelete.setVisibility(View.VISIBLE);
        llCheckAll.setVisibility(View.GONE);
        adapter.showTotalPrice();
    }

    /**
     * 校验数据
     */
    private void checkData() {
        if (adapter != null && adapter.getItemCount() > 0) {
            tvShopcartEdit.setVisibility(View.VISIBLE);
            ll_empty_shopcart.setVisibility(View.GONE);
        } else {
            ll_empty_shopcart.setVisibility(View.VISIBLE);
            tvShopcartEdit.setVisibility(View.GONE);
        }
    }


}




